package cn.q3c.parse.commons;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * download项目配置信息
 * @author wangxinning
 *
 */
public class Configuration {

	public final static Logger logger = Logger.getLogger(Configuration.class);
	
	private static Configuration instance=new Configuration();
	
	public XMLConfiguration config = null;
	
	private Configuration()
	{
		try {
			config = new XMLConfiguration("parse.xml");			
			//此处可以添加监听器，实现动态更新配置文件内容
			config.setReloadingStrategy(new FileChangedReloadingStrategy());

		} catch (ConfigurationException e) {
			logger.error("parse.xml文件加载失败", e);
		}
	}
	
	public static Configuration getInstance() {
		return instance;
	}

	//获取下载文件本地保存路径
	public String getStorePath()
	{
		String downloadPath = System.getProperty("parse.data.path");
		if( StringUtils.isBlank(downloadPath) )
		{
			downloadPath = config.getString("storepath");
		}
					
		return downloadPath;
	}
	
	//waittime
	public int getWaitTime()
	{
		return config.getInt("waittime",10);
	}
	
	
//	public String getName()
//	{
//		return config.getString("name");
//	}

	
	//项目列表
	public List<ConfigProject> getConfigProjectList()
	{
		int maxno = config.getMaxIndex("projects.project");
		
		List<ConfigProject> retList = new ArrayList<ConfigProject>();
		for( int i = 0; i <= maxno; i++ )
		{
			ConfigProject configProject = new ConfigProject();
			
			configProject.setDownloadProjectName(config.getString("projects.project(" + i + ").download_projectname"));
			configProject.setDescription(config.getString("projects.project(" + i + ").description"));
			configProject.setParseclass(config.getString("projects.project(" + i + ").parseclass"));
			configProject.setName(config.getString("projects.project(" + i + ").name"));			
			configProject.setProcessclassList(config.getStringArray("projects.project(" + i + ").processclasss.processclass"));
			configProject.setMaxtimelimit(config.getInt("projects.project(" + i + ").maxtimelimit"));
			configProject.setMaxprocesscount(config.getInt("projects.project(" + i + ").maxprocesscount"));
			configProject.setActive(config.getBoolean("projects.project(" + i + ").active", true ));//默认活动状态
			
			retList.add(configProject);
		}
		
		return retList;
	}
	
	
	
	
	private ComboPooledDataSource dataSource;
	
	public ComboPooledDataSource getDataSource() throws PropertyVetoException
	{
		if( this.dataSource == null )
		{
			this.dataSource = new ComboPooledDataSource();			
			this.dataSource.setUser( config.getString("database.user") );       
			this.dataSource.setPassword( config.getString("database.password") );       
			this.dataSource.setJdbcUrl( config.getString("database.jdbcUrl") ); 
			this.dataSource.setDriverClass( config.getString("database.driverClass") ); 
			this.dataSource.setInitialPoolSize( config.getInt("database.initialPoolSize")); 
			this.dataSource.setMinPoolSize( config.getInt("database.minPoolSize")); 
			this.dataSource.setMaxPoolSize( config.getInt("database.maxPoolSize"));
		}
		
		return this.dataSource;
	}
	
	
	private Connection getDBConnection() throws SQLException, PropertyVetoException
	{
		return this.getDataSource().getConnection();
	}
	
	
	
	public static void main( String[] args )
	{
		System.out.println(Configuration.getInstance().getConfigProjectList());
	}
}